﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataMappers;

using ConcreteMappersAndObjects.DomainObjects;
using System.Data;

namespace ConcreteMappersAndObjects.Mappers
{
    public class SubscriptionsDataMapper : AbstractDataMapper<SubscriptionKey, Subscription>
    {
        private IEntityMapper<int, Student> _studentsMapper;
        private IEntityMapper<int, CUnit> _cunitsMapper;

        public void SetStudentsMapper(IEntityMapper<int, Student> mapper)
        {
            _studentsMapper = mapper;
        }

        public void SetCUnitsMapper(IEntityMapper<int, CUnit> mapper)
        {
            _cunitsMapper = mapper;
        }

        protected override SubscriptionKey GetKeyFrom(System.Data.SqlClient.SqlDataReader sdr)
        {
            return new SubscriptionKey(sdr.GetInt32(0), sdr.GetInt32(1));
        }

        protected override string DoGetAllStatement()
        {
            return "select StudentID, CUnitID from SubscriptionCUStudent";
        }

        protected override string DoGetFindStatement()
        {
            return DoGetAllStatement() + " where StudentID = @StudentID and CUnitID = @CUnitID";
        }

        protected override void BindFindStatement(System.Data.SqlClient.SqlCommand cmd, SubscriptionKey key)
        {
            cmd.Parameters.AddWithValue("@StudentID", key.StudentKey);
            cmd.Parameters.AddWithValue("@CUnitID", key.CUnitKey);
        }

        protected override Subscription DoLoad(System.Data.SqlClient.SqlDataReader sdr)
        {
            ConcreteValueHolder<int, Student> student = new ConcreteValueHolder<int, Student>(sdr.GetInt32(0), _studentsMapper);
            ConcreteValueHolder<int, CUnit> cunit = new ConcreteValueHolder<int, CUnit>(sdr.GetInt32(1), _cunitsMapper);
            return new Subscription(GetKeyFrom(sdr), student, cunit);
        }

        protected override string GetUpdateCommand()
        {
            throw new NotImplementedException();
        }

        protected override void DoBindUpdate(System.Data.SqlClient.SqlCommand cmd, Subscription t)
        {
            throw new NotImplementedException();
        }

        protected override string GetInsertCommand()
        {
            return "insert into SubscriptionCUStudent (StudentID, CUnitID) values (@StudentID, @CUnitID)";
        }

        protected override void DoBindInsert(System.Data.SqlClient.SqlCommand cmd, Subscription t)
        {
            cmd.Parameters.Add("@StudentID", SqlDbType.Int).Value = t.Student.ID;
            cmd.Parameters.Add("@CUnitID", SqlDbType.Int).Value = t.CUnit.ID;

        }

        protected override string GetRemoveCommand()
        {
            return "delete from SubscriptionCUStudent where StudentID = @StudentID and CUnitID = @CUnitId";
        }

        protected override void DoBindRemove(System.Data.SqlClient.SqlCommand cmd, Subscription t)
        {
            cmd.Parameters.Add("@StudentID", SqlDbType.Int).Value = t.ID.StudentKey;
            cmd.Parameters.Add("@CUnitId", SqlDbType.Int).Value = t.ID.CUnitKey;
        }

        protected override void FinishUpdateObject(Subscription t)
        {
            
        }

        protected override void FinishNewObject(Subscription t, object id)
        {
            t.ID = new SubscriptionKey(t.Student.ID, t.CUnit.ID);
        }

        protected override void FinishDeleteObject(Subscription t)
        {
            
        }
    }
}
